package com.lie.flat.service.connect;

import com.lie.flat.entity.Card;
import com.lie.flat.entity.CardRequest;
import com.lie.flat.entity.CardResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CardCacheService {
    @Autowired
    ConnectService connectService;

    /**
     * 将卡信息列表存入Redis缓存
     * @param cardRequest
     * @return
     * @throws Exception
     */
    @Cacheable(value = "cardInfo", key = "#cardRequest.bizType")
    public List<Card> getCard(CardRequest cardRequest) throws Exception {
        CardResponse cardResponse = connectService.jsonConCard(cardRequest);
        if (!cardResponse.getCode().equals("C0001")) {
            throw new Exception("cardResponse is C0001, 外部api错误");
        }
        Map<String, List<Card>> result = cardResponse.getResult();
        return result.get("cardList");

    }
}
